package sk.fei.vrml.objects;

import javax.swing.DefaultListModel;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import sk.fei.vrml.main.Constants;

/**
 * Background { 
		skyColor [ 0 0 0 ] 
		skyAngle [ ] 
		groundColor [ ] 
		groundAngle [ ] 
		backUrl [ ] 
		bottomUrl [ ] 
		leftUrl [ ] 
		rightUrl [ ] 
		frontUrl [ ] 
		topUrl [ ] 
 * @author Noxtrip
 *
 */
public class VrmlBackground extends VrmlObject{
	private float [][]skyColors;
	private float []skyAngles;
	
	private float [][]groundColors;
	private float []groundAngles;
	
	
	
	public VrmlBackground() {
		super();
		this.type = Constants.Background;
	}

	public VrmlBackground(float [][]skyColors,float []skyAngles){
		this.type = Constants.Background;	
		this.skyColors = skyColors;
		this.skyAngles = skyAngles;
	}
	
	public VrmlBackground(float[][] skyColors, float[] skyAngles,
			float[][] groundColors, float[] groundAngles) {
		super();
		this.skyColors = skyColors;
		this.skyAngles = skyAngles;
		this.groundColors = groundColors;
		this.groundAngles = groundAngles;
	}

	public void setSkyColors( float [][]colors){
		skyColors = colors;
	}
	
	public void setSkyAngles(float []angles){
		skyAngles = angles;
	}
	
	public float[][] getSkyColors(){
		return skyColors;
	}
	
	public float[] getAngles(){
		return skyAngles;
	}
	
	public float[][] getGroundColors() {
		return groundColors;
	}

	public void setGroundColors(float[][] groundColors) {
		this.groundColors = groundColors;
	}

	public float[] getGroundAngles() {
		return groundAngles;
	}

	public void setGroundAngles(float[] groundAngles) {
		this.groundAngles = groundAngles;
	}

	public float[] getSkyAngles() {
		return skyAngles;
	}

	public void draw(GLAutoDrawable GLAutoDrawable){
		if (skyColors == null || skyAngles == null) return;
		// jednofarebne pozadie
		if (skyAngles.length == 0){
			GL gl = GLAutoDrawable.getGL();
			gl.glBegin(GL.GL_QUADS);
			gl.glColor3f(skyColors[0][0],skyColors[0][1],skyColors[0][2]);
			gl.glVertex2f(-1.0f, -1.0f);
			gl.glVertex2f(1.0f,-1.0f);

			gl.glColor3f(skyColors[0][0],skyColors[0][1],skyColors[0][2]);
			gl.glVertex2f(1.0f,1.0f);
			gl.glVertex2f(-1.0f,1.0f);
			gl.glEnd();
			return;
		}
		// viac farieb na pozadi
		GL gl = GLAutoDrawable.getGL();
		gl.glBegin(GL.GL_QUAD_STRIP);
		gl.glColor3f(skyColors[0][0],skyColors[0][1],skyColors[0][2]);
		gl.glVertex2f(-1.0f, 1.0f);
		gl.glVertex2f(1.0f,1.0f);
		for(int i=0; i<skyAngles.length; i++){
			float hight = -(skyAngles[i]/0.0157f-100f)/100f;
			gl.glColor3f(skyColors[i+1][0],skyColors[i+1][1],skyColors[i+1][2]);
			gl.glVertex2f(-1.0f, hight);
			gl.glVertex2f(1.0f,hight);
		}
		gl.glColor3f(skyColors[skyAngles.length][0],skyColors[skyAngles.length][1],skyColors[skyAngles.length][2]);
		gl.glVertex2f(-1.0f, -1.0f);
		gl.glVertex2f(1.0f,-1.0f);
		gl.glEnd();
	}
	
	public DefaultListModel getSkyColorsAsList(){
		DefaultListModel listModel = new DefaultListModel();
		if (skyColors == null) return listModel;
		if (skyColors.length == 1){
			listModel.addElement(new SkyColor(skyColors[0],0f));
			return listModel;
		}
		listModel.addElement(new SkyColor(skyColors[0],0f));
		for(int i=0; i<skyAngles.length; i++){
			listModel.addElement(new SkyColor(skyColors[i+1],skyAngles[i]));
		}
		return listModel;
	}
	
	public void loadColorsFromList(DefaultListModel model){
		int count = model.getSize();
		if (count == 0){
			skyColors = null;
			skyAngles = null;
			return;
		}
		skyColors = new float[count][3];
		skyAngles = new float[count-1];
		for (int i=0; i<count; i++){
			skyColors[i] = ((SkyColor)model.get(i)).getColor();
		}
		for (int i=0; i<count-1; i++){
			skyAngles[i] = ((SkyColor)model.get(i+1)).getAngle();
		}
	}
	
	public boolean isEmpty(){
		if (skyColors == null || skyColors.length == 0) return true;
		else return false;
	}
	
}
